package data;

import database.DataBaseConnection;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class CountRecordData {
    private String start;
    private String end;
    private String operator;
    private ArrayList<String> itemList = new ArrayList<>();

    public CountRecordData(String start, String end, String operator) {
        this.start = start;
        this.end = end;
        this.operator = operator;
        this.getItem();
    }

    private void getItem() {
        this.itemList.clear();
        Properties properties = new Properties();
        InputStreamReader inputStreamReader = null;
        try {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream("dimension.properties");
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            properties.load(inputStreamReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration<?> enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            String key = (String) enumeration.nextElement();
            itemList.add(key);
        }
        if (inputStreamReader != null) {
            try {
                inputStreamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String countEncryptionData() {
        return this.countRecordData("encryption");
    }

    public String countDecryptionData() {
        return this.countRecordData("decryption");
    }

    public String countVerificationData() {
        return this.countRecordData("verification");
    }

    public String countSignatureData() {
        return this.countRecordData("signature");
    }

    public String countSigncryptionData() {
        return this.countRecordData("signcryption");
    }

    public String countUnsigncryptionData() {
        return this.countRecordData("unsigncryption");
    }

    public String countMacData() {
        return this.countRecordData("mac");
    }

    public String countAEData() {
        return this.countRecordData("authenticated_encryption");
    }

    public String countDVData() {
        return this.countRecordData("decryption_verification");
    }

    private String countRecordData(String table) {
        String select, where;
        select = "select count(*) as count,";
        if (this.operator.equals("")) {
            where = " from " + table + " where execute_time < '" + this.end + "' and execute_time > '"
                    + this.start + "' ";
        } else {
            where = " from " + table + " where execute_time < '" + this.end + "' and execute_time > '"
                    + this.start + "' and " + "operator = '" + this.operator + "' ";
        }
        return this.getJsonData(select, where);
    }

    private String getJsonData(String select, String where) {
        Connection connection = DataBaseConnection.getConnection();
        JSONArray jsonArray = new JSONArray();
        String sql;
        for (String item : itemList) {
            sql = select + item + where + "group by " + item;
            ResultSet resultSet = null;
            if (connection != null) {
                resultSet = DataBaseConnection.query(connection, sql);
            }
            JSONObject jsonObject = new JSONObject();
            HashMap<String, Integer> map = new HashMap<>();
            try {
                while (resultSet != null && resultSet.next()) {
                    String name = resultSet.getString(item);
                    int count = resultSet.getInt("count");
                    map.put(name, count);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            jsonObject.put(item, map);
            jsonArray.add(jsonObject);
        }
        if (connection != null) {
            DataBaseConnection.closeConnection(connection);
        }
        return jsonArray.toString();
    }
}
